﻿using CountTools;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace CountTools
{
    public partial class CodeCountRuleForm : Form
    {
        private List<string> elementTypes;
        public string saves2 = null;
        private string path;
        public static List<string[]> strlist = new List<string[]>();
        private XmlDocument xml;

        public CodeCountRuleForm(List<string> elementTypes, string path)
        {
            this.elementTypes = elementTypes;
            this.path = path;
            InitializeComponent();
        }

        private void CodeCountRuleForm_Load(object sender, EventArgs e)
        {
            treeList1.Enabled = true;

            //编码
            TreeListNode nodeA = treeList1.Nodes.Add("A土石方工程", "");
            TreeListNode nodeB = treeList1.Nodes.Add("B地基处理与边坡支护工程", "");
            TreeListNode nodeC = treeList1.Nodes.Add("C桩基工程", "");
            TreeListNode nodeD = treeList1.Nodes.Add("D砌筑工程", "");
            TreeListNode nodeE = treeList1.Nodes.Add("E混凝土及钢筋混凝土工程", "");
            TreeListNode nodeF = treeList1.Nodes.Add("F金属结构工程", "");
            TreeListNode nodeG = treeList1.Nodes.Add("G木结构工程", "");
            TreeListNode nodeH = treeList1.Nodes.Add("H门窗工程", "");
            TreeListNode nodeJ = treeList1.Nodes.Add("J屋面及防水工程", "");

            XmlDocument xml = new XmlDocument();
            path = path.Replace("算量信息", "清单规则");
            xml.Load(path);
            this.xml = xml;

            foreach (string elementType in elementTypes)
            {
                string temp = GetCountData(elementType.Split(' ')[1], path);
                string projectName = temp.Split('$')[0];
                string nodeName = temp.Split('$')[1];
                TreeListNode node1 = null;
                foreach (TreeListNode node in nodeE.Nodes)
                {
                    if (node.GetDisplayText(treeListColumn1) == nodeName)
                    {
                        node1 = node;
                        break;
                    }
                }
                if (node1 == null)
                {
                    node1 = nodeE.Nodes.Add(nodeName, "");
                }

                XmlNode childNode = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName).SelectSingleNode(nodeName);
                XmlNodeList childNodeList = childNode.ChildNodes;
                foreach (XmlNode child2Node in childNodeList)
                {
                    if (child2Node.Name == elementType.Split(' ')[1])
                    {
                        node1.Nodes.Add(elementType.Split(' ')[0], elementType.Split(' ')[1], child2Node.SelectSingleNode("计量单位").InnerText);
                        break;
                    }
                }
                node1.ExpandAll();
            }
            nodeE.ExpandAll();


        }

        private string GetCountData(string temp, string path)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(path);
            XmlNode childNode = xml.SelectSingleNode("工程量计算规范");

            foreach (XmlNode xmlNode in childNode.ChildNodes)
            {
                foreach (XmlNode xmlElement in xmlNode.ChildNodes)
                {
                    foreach (XmlNode childElem in xmlElement.ChildNodes)
                    {
                        if (childElem.Name == temp)
                        {
                            return xmlNode.Name + "$" + xmlElement.Name;
                        }
                    }
                }
            }
            return "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(path);

            var nodes = treeList1.Nodes;
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Nodes.Count > 0)
                {
                    string projectName = nodes[i].GetDisplayText(treeListColumn1);
                    var childNodes = nodes[i].Nodes;
                    {
                        for (int j = 0; j < childNodes.Count; j++)
                        {
                            string nodeName = childNodes[j].GetDisplayText(treeListColumn1); ;
                            var childChildNodes = childNodes[j].Nodes;
                            for (int k = 0; k < childChildNodes.Count; k++)
                            {
                                string codeName = childChildNodes[k].GetDisplayText(treeListColumn2);
                                string keyWords = childChildNodes[k].GetDisplayText(treeListColumn3);
                                XmlNode childNode = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName).SelectSingleNode(nodeName).SelectSingleNode(codeName).SelectSingleNode("计量单位");
                                childNode.InnerText = keyWords;
                            }
                        }
                    }
                }
            }
            xml.Save(path);
            MessageBox.Show("保存规则成功！！");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            var nodes = treeList1.Nodes;
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Nodes.Count > 0)
                {
                    string projectName = nodes[i].GetDisplayText(treeListColumn1);
                    var childNodes = nodes[i].Nodes;
                    {
                        for (int j = 0; j < childNodes.Count; j++)
                        {
                            string nodeName = childNodes[j].GetDisplayText(treeListColumn1); ;
                            var childChildNodes = childNodes[j].Nodes;
                            for (int k = 0; k < childChildNodes.Count; k++)
                            {
                                string code = childChildNodes[k].GetDisplayText(treeListColumn1);
                                string codeName = childChildNodes[k].GetDisplayText(treeListColumn2);
                                string keyWords = childChildNodes[k].GetDisplayText(treeListColumn3);
                                list.Add(code + " " + codeName + "-" + keyWords);
                            }
                        }
                    }
                }
            }

            CodeList = list;


            this.Close();
        }

        public List<string> CodeList { get; internal set; }

        private void treeList1_ShowingEditor(object sender, CancelEventArgs e)
        {
            TreeList view = sender as TreeList;
            if (view.FocusedColumn.FieldName == "清单工程量" && !string.IsNullOrEmpty(view.FocusedNode.GetDisplayText(treeListColumn2)))
            {
                TreeListNode classNode = view.FocusedNode.ParentNode;
                TreeListNode projectNode = classNode.ParentNode;

                string projectName = projectNode.GetDisplayText(treeListColumn1);
                string className = classNode.GetDisplayText(treeListColumn1);
                string codeName = view.FocusedNode.GetDisplayText(treeListColumn2);
                //string character = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName).SelectSingleNode(className).SelectSingleNode(codeName).SelectSingleNode("项目特征").InnerText;
                string characters = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName).SelectSingleNode(className).SelectSingleNode(codeName).SelectSingleNode("可选单位").InnerText;

                int index = view.FocusedNode.Id;
                for (int i = 0; i < index + 1; i++)
                {
                    if (i == index)
                    {
                        repositoryItemComboBox1.ReadOnly = false;
                        repositoryItemComboBox1.Items.Clear();
                        string[] tempStr = characters.Split('_');
                        repositoryItemComboBox1.Items.AddRange(tempStr);
                    }
                }
                //treeList1.FocusedNode.SetValue(treeListColumn3, character);
            }
            else
            {
                int index = view.FocusedNode.Id;
                for (int i = 0; i < index + 1; i++)
                {
                    if (i == index)
                    {
                        repositoryItemComboBox1.ReadOnly = true;

                    }
                }
            }
        }

        private void treeList1_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e)
        {
            if (e.Column == treeListColumn3)
            {
                if (e.CellValue == null)
                {
                    e.Appearance.BackColor = System.Drawing.Color.White;
                    e.Appearance.Options.UseBackColor = true;
                }
            }
        }
    }
}
